#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>


#define INFO_SIZE (1024000)


void send_data(int sockfd);


int main()
{
	int sockfd;

	struct sockaddr_in serv_addr;

	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	memset(&serv_addr, 0, sizeof(serv_addr));

	serv_addr.sin_family = AF_INET;
	// serv_addr.sin_addr.s_addr = "169.254.156.155";
	serv_addr.sin_port = htons(15555);
	inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
	int conn_rt = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
	if (conn_rt < 0) {
		fprintf(stdout, "connect failed\n"); 
	}

	send_data(sockfd);
	exit(0);
}


void send_data(int sockfd) {
	char *info;
	info = malloc(INFO_SIZE + 1);

	for (int i = 0; i < INFO_SIZE; ++i) {
		info[i] = 'a';
	}

	info[INFO_SIZE] = "\0";

	const char *ci;
	ci = info;
	size_t remain_len = strlen(info);

	while (remain_len) {
		int write_n = send(sockfd, ci, remain_len, 0);
		fprintf(stdout, "send into buffer %ld \n", write_n);

		if (write_n <= 0) {
			fprintf(stdout, "send failed\n");
			return;
		}

		remain_len -= write_n;
		ci += write_n;
	}
	return;
}